home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Info-Mac 3
/
Info_Mac_1994-01.iso
/
HyperCard
/
RTF XCMDs 1.3
/
lists.c
< prev
next >
Wrap
Text File
|
1993-06-20
|
3KB
|
199 lines
/*
* This software is copyright 1992 by Robert Morris.
* You may freely redistribute this software as shareware
* if you do so in the same form as you got it. If you find
* this software useful, please send $12 to:
* Robert Morris
* P.O. Box 1044
* Harvard Square Station
* Cambridge, MA 02238
* ecognome@aol.com
* If you incorporate any of this software in any kind of
* commercial product, please send $2 per copy distributed
* to the above address.
*/
#include <stdlib.h>
#include <string.h>
#include "lists.h"
long
NewList(h, lp)
char **h;
struct list *lp;
{
if(h){
lp->h = h;
lp->size = GetHandleSize(h);
lp->ptr = 0;
} else {
lp->size = 256;
lp->h = NewHandle(lp->size);
if(lp->h == 0)
return(-1);
lp->ptr = 0;
(*(lp->h))[0] = '\0';
}
return(0);
}
void
FreeList(lp)
struct list *lp;
{
if(lp->h)
DisposHandle(lp->h);
lp->h = 0;
}
/*
* get rid of excess storage at the end of the handle.
*/
long
TrimList(lp)
struct list *lp;
{
if(lp->h == 0)
return(-1);
SetHandleSize(lp->h, lp->ptr + 1);
lp->size = GetHandleSize(lp->h);
if(lp->size > 0)
(*(lp->h))[lp->size - 1] = '\0';
return(lp->ptr);
}
long
PeekListLine(lp, buf)
register struct list *lp;
register char *buf;
{
/* THINK C supplies 5 data and 3 pointer register vars */
register int c;
register long i;
register char *ptr;
buf[0] = '\0';
if(lp->h == 0 || *(ptr = (*(lp->h))+lp->ptr) == '\0')
return(-1);
i = 0;
while((c = *(ptr++)) != '\0' && c != '\r'){
if(i < 255)
buf[i++] = c;
}
if(c == '\0')
--ptr;
buf[i] = '\0';
return(ptr - (*(lp->h) + lp->ptr));
}
/*
* skip back a line.
* assumes we're at the start of a line, just after the \r.
*/
void
RewindListLine(lp)
struct list *lp;
{
if(lp->h == 0)
return;
lp->ptr -= 2;
while(lp->ptr >= 0 && (*(lp->h))[lp->ptr] != '\r')
lp->ptr -= 1;
if(lp->ptr < 0)
lp->ptr = 0;
else
lp->ptr += 1;
}
/*
* reads the next line from a list into buf. returns -1 on EOF.
*/
long
ReadListLine(lp, buf)
struct list *lp;
char buf[];
{
long n;
n = PeekListLine(lp, buf);
if(n < 0)
return(-1);
lp->ptr += n;
return(strlen(buf));
}
/*
* appends some text to a list. returns -1 on error (out of memory).
*/
long
AppendList(lp, buf)
struct list *lp;
char buf[];
{
long len;
if(lp->h == 0)
return(-1);
len = strlen(buf);
if(lp->ptr + len + 1 > lp->size){
if(lp->size > 4096)
lp->size += (len + 4096);
else
lp->size += (len + lp->size);
SetHandleSize(lp->h, lp->size);
if(MemError() != noErr){
DisposHandle(lp->h);
lp->h = 0;
lp->size = -1;
lp->ptr = 0;
return(-1);
}
}
strcpy((*(lp->h)) + lp->ptr, buf);
lp->ptr += len;
return(len);
}
int
_ALC(lp, c)
struct list *lp;
{
char buf[2];
buf[0] = c;
buf[1] = '\0';
AppendList(lp, buf);
}
/*
* get the itm'th sep-separated item from in[].
* 1 origin.
* out[] should hold 256 chars.
*/
char *
item(itm, in, out, sep)
register int itm, sep;
register char *in, *out;
{
register int i, j;
for(i = 0; itm > 1 && in[i]; i++){
if(in[i] == sep)
--itm;
}
j = 0;
if(itm == 1){
while(in[i] && in[i] != sep && j < 255){
out[j++] = in[i++];
}
}
out[j] = '\0';
return(out);
}